// pages/alumni-detail/alumni-detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    alumniInfo: {
      id: '',
      name: '',
      company: '',
      position: '',
      email: '',
      phone: '',
      industry: '',
      visibility: 'private',
      hasOpenings: false,
      showContact: false,
      isAlumniCompany: false,
      contactInfo: {
        email: '',
        phone: ''
      }
    },
    hasRequested: false,
    contactStatus: 'pending',
    isCurrentUser: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!options || !options.id) {
      wx.showToast({
        title: '缺少必要参数',
        icon: 'none'
      });
      wx.navigateBack();
      return;
    }
    const id = options.id;
    this.getAlumniDetail(id);
  },

  // 获取校友详情
  getAlumniDetail(id) {
    wx.showLoading({ title: '加载中...' });
    wx.request({
      url: `http://localhost:3000/api/alumni/${id}`,
      method: 'GET',
      success: (res) => {
        console.log('API响应:', res);
        if (res.statusCode === 200 && res.data && res.data.success) {
          const data = res.data.data;
          this.setData({
            alumniInfo: {
              ...this.data.alumniInfo, // 保留默认值
              id: data.id,
              name: data.name || '未填写',
              company: data.company || '未填写',
              position: data.position || '未填写',
              email: data.email || '',
              phone: data.phone || '',
              industry: data.industry || '未填写',
              visibility: data.visibility || 'private',
              hasOpenings: data.hasOpenings || false,
              showContact: data.visibility === 'public' || data.isCurrentUser,
              isAlumniCompany: data.company && getApp().globalData?.currentUserCompany && 
                data.company.toLowerCase() === (getApp().globalData.currentUserCompany || '').toLowerCase(),
              hasAlumniInCompany: data.company && 
                getApp().globalData?.currentUserCompany && 
                data.company.toLowerCase() === (getApp().globalData.currentUserCompany || '').toLowerCase(),
              contactInfo: {
                email: data.email || '',
                phone: data.phone || ''
              }
            },
            isCurrentUser: data.isCurrentUser
          });
        } else {
          console.error('API返回数据格式错误:', res);
          wx.showToast({
            title: '数据格式错误',
            icon: 'none'
          });
        }
        wx.hideLoading();
      },
      fail: (err) => {
        console.error('请求失败:', err);
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        });
        wx.hideLoading();
      }
    });
  },

  // 请求联系方式
  requestContact() {
    wx.showLoading({ title: '请求中...' });
    wx.request({
      url: `http://localhost:3000/api/alumni/${this.data.alumniInfo.id}/request-contact`,
      method: 'POST',
      data: {
        requesterId: getApp().globalData.userId,
        alumniId: this.data.alumniInfo.id,
        message: `请求查看${this.data.alumniInfo.name}的联系方式`
      },
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            hasRequested: true,
            contactStatus: res.data.status
          });
          wx.showToast({
            title: '请求已发送',
            icon: 'success'
          });
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
        wx.showToast({
          title: '请求失败，请重试',
          icon: 'none'
        });
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
